<?php

// +----------------------------------------------------------------------
// | 订单处理接口
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\api\v3;

use app\event\pos\PosOrder as PosOrderEvent;
use app\model\pos\PosOrder as PosOrderModel;
use app\event\store\Order as OrderEvent;
use app\event\store\DepositOrder as DepositOrderEvent;
use app\model\store\Order as OrderModel;

class Order extends Common
{


    /**
     * 再次支付
     */
    public function repay()
    {
        if ($this->request->isPost()) {
            $event = new PosOrderEvent();
            $data = $event->repay($this->request->post(), $this->user);

        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return json($data);
    }



    /**
     * 取消订单
     */
    public function cancel()
    {
        if ($this->request->isPost()) {
            $event = new PosOrderEvent();
            $data = $event->buyCancel($this->request->post(), $this->user);
        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return json($data);
    }


    /**
     * 确认收货
     */
    public function confirm()
    {
        if ($this->request->isPost()) {
            $event = new PosOrderEvent();
            $data = $event->confirm($this->request->post(), $this->user, 'user');
        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return json($data);
    }

    /**
     * 预览订单
     */
    public function preview()
    {
        $param = $this->request->param('', null, 'strip_tags');
        if (isset($param['good_id'])) {
            $event = new OrderEvent();
            $data = $event->preview($this->request->param('', null, 'strip_tags'), $this->user);
        }else{
            $event = new PosOrderEvent();
            $data = $event->preview($this->request->param('', null, 'strip_tags'), $this->user);
        }


        return $this->autoAjax($data);
    }

    /**
     * 创建订单
     */
    public function create()
    {
        $param = $this->request->param('', null, 'strip_tags');
        if (isset($param['good_id'])) {
            $event = new OrderEvent();
            $data = $event->create($this->request->param('', null, 'strip_tags'), $this->user);
        }else{
            $order = new PosOrderEvent();
            $data = $order->create($this->request->param('', null, 'strip_tags'), $this->user);
        }
        return $this->autoAjax($data);
    }

    public function buyStatistics()
    {
        $data['code'] = 0;
        $data['data']['wait_send'] = $this->user->orders()->where('status', 2)->count('id');
        $data['data']['wait_sign'] = $this->user->orders()->where('status', 3)->count('id');
        $data['data']['wait_send'] = ($data['data']['wait_send'] > 999) ? '999+' : $data['data']['wait_send'];
        $data['data']['wait_sign'] = ($data['data']['wait_sign'] > 999) ? '999+' : $data['data']['wait_sign'];
        return $this->autoAjax($data);
    }

    /**
     * 卖出统计
     */
    public function saleStatistics()
    {
        $data['code'] = 0;
        $data['data']['wait_send'] = $this->user->sales()->where('status', 2)->count('id');
        $data['data']['wait_sign'] = $this->user->sales()->where('status', 3)->count('id');
        $data['data']['wait_send'] = ($data['data']['wait_send'] > 999) ? '999+' : $data['data']['wait_send'];
        $data['data']['wait_sign'] = ($data['data']['wait_sign'] > 999) ? '999+' : $data['data']['wait_sign'];
        return $this->autoAjax($data);
    }

    /**
     * 获取卖出订单
     */
    public function lists()
    {

        $status = $this->request->get('status', 0);

        $data['lists'] = PosOrderModel::with(['pos_vips'=>['cover']])
            ->where('user_id',$this->user['id'])
            ->when($status, function ($query) use ($status) {
                $query->where('status',$status==4?5:$status);
            })
            ->order('create_time DESC')->paginate(5);

        return $this->successAjax($data);
    }


    /**
     * 获取卖出订单
     */
    public function lists2()
    {

        $status = $this->request->get('status', 0);

        $data['lists'] = OrderModel::with(['goods'=>['cover']])
            ->where('user_id',$this->user['id'])
            ->where('type',1)
            ->when($status, function ($query) use ($status) {
                $query->where('status',$status==4?5:$status);
            })
            ->order('create_time DESC')->paginate(5);

        return $this->successAjax($data);
    }


    /**
     * 获取卖出订单
     */
    public function deposit()
    {
        $order = new DepositOrderEvent();
        $data = $order->create($this->request->param('', null, 'strip_tags'), $this->user);
        return $this->autoAjax($data);
    }


}
